package ink.xiaobaibai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ink.xiaobaibai.dao.FaultTerminalDao;
import ink.xiaobaibai.entity.FaultTerminal;
import ink.xiaobaibai.entity.OxygenDevice;
import ink.xiaobaibai.entity.OzoneDevice;
import ink.xiaobaibai.enumOxy.DeviceStatus;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IFaultTerminalService;
import ink.xiaobaibai.service.IOxygenDeviceService;
import ink.xiaobaibai.service.IOzoneDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2021-06-03
 */
@Service
public class FaultTerminalServiceImpl extends ServiceImpl<FaultTerminalDao, FaultTerminal> implements IFaultTerminalService {

    @Autowired
    private IOzoneDeviceService ozoneDeviceService;

    @Autowired
    private IOxygenDeviceService oxygenDeviceService;

    @Autowired
    private IAdminUserService adminUserService;

    @Override
    public void onePayFaultToOzone(String deviceCode) {
        //将当前机器故障即可
        UpdateWrapper<OzoneDevice> u1 = new UpdateWrapper<>();
        u1.set("status", DeviceStatus.FAULT.getStatus());
        u1.set("device_code", deviceCode);
        this.ozoneDeviceService.update(u1);
        QueryWrapper<OzoneDevice> q1 = new QueryWrapper<>();
        q1.select("admin_user_id");
        q1.eq("device_code", deviceCode);
        Integer adminUserId = this.ozoneDeviceService.getOne(q1).getAdminUserId();
        //存入
        this.save(new FaultTerminal(deviceCode, "定点臭氧机器故障", adminUserId, this.adminUserService.getDepId(adminUserId)));
    }

    @Override
    public void onePayFaultToOxygen(String deviceCode) {
        //将当前机器和当前机器下的所有臭氧
        QueryWrapper<OxygenDevice> q1 = new QueryWrapper<>();
        q1.select("id", "admin_user_id");
        q1.eq("device_code", deviceCode);
        OxygenDevice oxygenDevice = this.oxygenDeviceService.getOne(q1);
        Integer id = oxygenDevice.getId();

        UpdateWrapper<OxygenDevice> u1 = new UpdateWrapper<>();
        u1.set("status", DeviceStatus.FAULT.getStatus());
        u1.eq("id", id);
        this.oxygenDeviceService.update(u1);

        UpdateWrapper<OzoneDevice> u2 = new UpdateWrapper<>();
        u2.set("status", DeviceStatus.FAULT.getStatus());
        u2.eq("oxygen_id", id);
        this.ozoneDeviceService.update(u2);

        //存入
        QueryWrapper<OzoneDevice> q2 = new QueryWrapper<>();
        q2.select("device_code");
        q2.eq("oxygen_id", id);
        Integer depId = this.adminUserService.getDepId(oxygenDevice.getAdminUserId());
        List<FaultTerminal> faultTerminalList = this.ozoneDeviceService.list(q2).stream().map(o -> new FaultTerminal(o.getDeviceCode(), "定点:氧气机连锁反应故障", oxygenDevice.getAdminUserId(), depId)).collect(Collectors.toList());
        faultTerminalList.add(new FaultTerminal(deviceCode, "定点氧气机器故障", oxygenDevice.getAdminUserId(), depId));
        this.saveBatch(faultTerminalList);
    }

}
